package com.zxgllhh.generator;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;

import com.zxgllhh.config.FreemarkerManager;
import com.zxgllhh.domain.Field;
import com.zxgllhh.domain.Table;
import com.zxgllhh.utils.PropertyUtils;

import freemarker.template.Template;
/**
 * 
 * @author Zhao,XingGuo
 *
 */
public class MapsGenarator {

	public static void goByOracle(Table table) throws Exception {
		// init
		String projectName = PropertyUtils.getProperty("projectName");
		String companyName = PropertyUtils.getProperty("companyName");
		List<Field> fieldList = table.getFieldList();
		//prepare
		Map<String, Object> map = new HashMap<String, Object>();
		Template template = FreemarkerManager.getOracleMapsTemplate();
		map.put("projectName", projectName);
		map.put("companyName", companyName);
		map.put("fieldList", fieldList);
		map.put("table", table);
		//execute  HOUSE_WORKER_SqlMap.xml
		FileUtils.forceMkdir(new File("E://generatorCode/src/com/"+companyName+"/" + projectName + "/dao/ibatis/maps"));
		File file = new File("E://generatorCode/src/com/"+companyName+"/" + projectName + "/dao/ibatis/maps/" + table.getTableName() + "_SqlMap.xml");
		//Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
		Writer writer = new FileWriter(file);
		template.process(map, writer);
	}
	
	public static void goByMySql(Table table) throws Exception {
		// init
		String projectName = PropertyUtils.getProperty("projectName");
		String companyName = PropertyUtils.getProperty("companyName");
		List<Field> fieldList = table.getFieldList();
		//prepare
		Map<String, Object> map = new HashMap<String, Object>();
		Template template = FreemarkerManager.getMysqlMapsTemplate();
		map.put("projectName", projectName);
		map.put("companyName", companyName);
		map.put("fieldList", fieldList);
		map.put("table", table);
		//execute  HOUSE_WORKER_SqlMap.xml
		//FileUtils.forceMkdir(new File("E://generatorCode/src/com/"+companyName+"/" + projectName + "/dao/ibatis/maps"));
		File file = new File("E://generatorCode/src/com/"+companyName+"/" + projectName + "/dao/ibatis/maps/" + table.getTableName() + "_SqlMap.xml");
		//Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
		Writer writer = new FileWriter(file);
		template.process(map, writer);
	}
	
	public static void go(Table table , String databaseName) throws Exception {
		// 
		if("oracle".equalsIgnoreCase(databaseName)){
			MapsGenarator.goByOracle(table);
		}else if("mysql".equalsIgnoreCase(databaseName)){
			MapsGenarator.goByMySql(table);
		}else{
			System.out.println("请正确填写databaseName，目前只能支持oracle或者mysql请任意选择一个");
			System.exit(0);
		}
		
	}

}
